package com.lp.module_home_b.fragment;

import android.Manifest;
import android.os.Environment;
import android.widget.Button;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.lp.module_home_b.R;
import com.lp.module_home_b.R2;
import com.lp.module_home_b.view.FlashChargeView;
import com.lp.module_lib.api.BasePresenter;
import com.lp.module_lib.base.BaseARouter;
import com.lp.module_lib.base.BaseFragment;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 作者：created by LP_TREAM on 2020/11/25 22:28
 * 邮箱：806875201@qq.com
 */

@Route(path = BaseARouter.RouterJhxw)
public class HomeBFragment extends BaseFragment {

    @BindView(R2.id.chargeView)
    FlashChargeView mChargeView;
    @BindView(R2.id.btn_get)
    Button mBtnGet;

    private int mNumber = 0;

    private Boolean isShow = true;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_b_activity_home;
    }

    @Override
    protected void initData() {
        isShow = true;
        mChargeView.startChargeAnimator();
        start();
    }

    private void start() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isShow) {
                    mNumber++;
                }
                if (mNumber > 100) {
                    mNumber = 0;
                }
                mChargeView.setPower(mNumber);
                start();
            }
        }, 1000);
    }


    @Override
    public void onHiddenChanged(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isShow = !isVisibleToUser;
        if (!isVisibleToUser) {
            mChargeView.startChargeAnimator();
        } else {
            mChargeView.endChargeAnimator();
        }
    }

    @OnClick(R2.id.btn_get)
    public void onViewClicked() {
        getP();
    }


    private void getP(){
        RxPermissions rxPermissions = new RxPermissions(getActivity());
        rxPermissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,//写
                Manifest.permission.READ_EXTERNAL_STORAGE//读
        ).subscribe(granted -> {
            if (granted) {

                String baseFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
                //截屏图片文件存储路径
                String baseImageFileName = baseFileName + "/myfile/systeminfo.cfg";
                getDataMsg(new File(baseImageFileName), 1, String.class);
            } else {
                ToastUtils.show("读写权限未获取成功!!");
            }
        });
    }



    public static Object getDataMsg(File cfgFile, int dataKey, Class<?> classzz) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(cfgFile);
            ObjectInputStream ois = new ObjectInputStream(fis);

            Map<Integer, Object> maps = (Map<Integer, Object>) ois.readObject();
            for (Integer key : maps.keySet()) {
                System.out.println("key:" + key + ",value:" + maps.get(key));
            }
            if (classzz == Integer.class) {
                return maps.get(dataKey);
            } else if (classzz == String.class) {
                return maps.get(dataKey);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
